package com.ruoyi.docManager.controller;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.docManager.domain.DocManager;
import com.ruoyi.docManager.domain.SendDocManager;
import com.ruoyi.docManager.domain.SignOperate;
import com.ruoyi.docManager.service.IDocManagerService;
import com.ruoyi.docManager.service.ISendDocManagerService;
import com.ruoyi.docManager.service.ISignOperateService;
import java.util.List;

import com.ruoyi.docManager.domain.SendDocManager;
import com.ruoyi.docManager.service.ISendDocManagerService;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import java.util.Date;


/**
 * 公文签收Controller
 *
 * @author kafka
 * @date 2022-11-23
 */
@Controller
@RequestMapping("/docManager/manager")
public class DocManagerController extends BaseController {
    private String prefix = "docManager/manager";

    @Autowired
    private IDocManagerService docManagerService;
    @Autowired
    private ISendDocManagerService sendDocManagerService;
    @Autowired
    private ISysDeptService deptService;

    @RequiresPermissions("docManager:manager:view")
    @GetMapping()
    public String manager() {
        return prefix + "/manager";
    }

    /**
     * 查询公文签收列表
     */
    @RequiresPermissions("docManager:manager:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DocManager docManager) {
        startPage();
        List<DocManager> list = docManagerService.selectDocManagerList(docManager);
        return getDataTable(list);
    }

    /**
     * 导出公文签收列表
     */
    @RequiresPermissions("docManager:manager:export")
    @Log(title = "公文签收", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DocManager docManager) {
        List<DocManager> list = docManagerService.selectDocManagerList(docManager);
        ExcelUtil<DocManager> util = new ExcelUtil<DocManager>(DocManager.class);
        return util.exportExcel(list, "公文签收数据");
    }

    /**
     * 新增公文签收
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存公文签收
     */
    @RequiresPermissions("docManager:manager:add")
    @Log(title = "公文签收", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DocManager docManager) {
        return toAjax(docManagerService.insertDocManager(docManager));
    }

    /**
     * 修改公文签收
     */
    @RequiresPermissions("docManager:manager:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        DocManager docManager = docManagerService.selectDocManagerById(id);
        mmap.put("docManager", docManager);
        return prefix + "/edit";
    }

    /**
     * 修改保存公文签收
     */
    @RequiresPermissions("docManager:manager:edit")
    @Log(title = "公文签收", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DocManager docManager) {
        return toAjax(docManagerService.updateDocManager(docManager));
    }

    /**
     * 删除公文签收
     */
    @RequiresPermissions("docManager:manager:remove")
    @Log(title = "公文签收", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(docManagerService.deleteDocManagerByIds(ids));
    }




    @Autowired
    private ISignOperateService signOperateService;

    /**@Description: 签收确认
     * @Author: zyx
     * @Date: 2022/11/28 10:54
     * @version v1.0
     */
    @PostMapping("/signDoc")
    @ResponseBody
    public AjaxResult signDoc(SignOperate signOperate) {
        System.out.println(signOperate.toString());
        Date date=new Date();
        signOperate.setReceiveDate(date);
        return toAjax(signOperateService.insertSignOperate(signOperate));
    }
    /**
    * 转发公文详情
    *
    * */
    @RequiresPermissions("docManager:manager:sendDoc")
    @GetMapping("/{docId}/sendDoc")
    public String sendDoc(@PathVariable Long docId,ModelMap mmp){
        mmp.put("docId",docId);
        return prefix+"/sendDoc";
    }

    @RequiresPermissions("docManager:manager:sendDocList")
    @GetMapping("{docId}/sendDocList")
    @ResponseBody
    public TableDataInfo sendDoc(@PathVariable Long docId){

        startPage();
        List<SendDocManager> sendDocManagers = sendDocManagerService.selectSendDocManagerList(docId);
        return getDataTable(sendDocManagers);
    }


    /**
     * 新增公文转发
     */
    @RequiresPermissions("docManager:manager:edit")
    @GetMapping("/addSendDoc/{id}")
    public String addSendDoc(@PathVariable("id") Long id, ModelMap mmap)
    {
        DocManager docManager = docManagerService.selectDocManagerById(id);
        mmap.put("docManager", docManager);
        mmap.put("dept",docManager.getDept());
        return prefix + "/addSendDoc";
    }
    /**
     * 保存新增转发公文
     */
    @RequiresPermissions("docManager:manager:sendDoc")
//    @Log(title = "公文签收", businessType = BusinessType.UPDATE)
    @PostMapping("/addSendDoc")
    @ResponseBody
    public AjaxResult sendDocSave(SendDocManager sendDocManager)
    {
        System.out.println(sendDocManager.getDocId());
        return toAjax(sendDocManagerService.insertSendDocManager(sendDocManager));
    }
    /**
     * 选择部门树
     *
     * @param deptId 部门ID
     */
    @RequiresPermissions("system:user:list")
    @GetMapping("/selectDeptTree/{deptId}")
    public String selectDeptTree(@PathVariable("deptId") Long deptId, ModelMap mmap)
    {
        mmap.put("dept", deptService.selectDeptById(deptId));
        return prefix + "/deptTree";
    }

}
